package calendar;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/* this class provides the panel used to connect to calendars */
class ConnectPanel extends CalendarPanel implements DocumentListener {

  private static final long serialVersionUID = 230498234L;
  TextInputPanel userIdTextPanel;
  JButton connectButton;
  JButton disconnectButton;

  public ConnectPanel()
  {
    Dimension userIdTextPanelSize = new Dimension( 120, 20 );

    userIdTextPanel = new TextInputPanel( "User:", 5 );
    userIdTextPanel.setMaximumSize( userIdTextPanelSize );
    userIdTextPanel.setMinimumSize( userIdTextPanelSize );
    userIdTextPanel.setPreferredSize( userIdTextPanelSize );
    userIdTextPanel.addDocumentListener( this );

    Dimension connectButtonSize = new Dimension( 92, 20 );
    Dimension disconnectButtonSize = new Dimension( 108, 20 );

    connectButton = new JButton( "Connect" );
    connectButton.setMaximumSize( connectButtonSize );
    connectButton.setMinimumSize( connectButtonSize );
    connectButton.setPreferredSize( connectButtonSize );
    connectButton.setToolTipText( "Connect to a Calendar" );
    connectButton.addActionListener( this );

    disconnectButton = new JButton( "Disconnect" );
    disconnectButton.setMaximumSize( disconnectButtonSize );
    disconnectButton.setMinimumSize( disconnectButtonSize );
    disconnectButton.setPreferredSize( disconnectButtonSize );
    disconnectButton.setToolTipText( "Disconnect from current Calendar" );
    disconnectButton.addActionListener( this );

    setLayout( new BoxLayout( this, BoxLayout.X_AXIS ) );

    add( Box.createHorizontalStrut( 5 ) );
    add( userIdTextPanel );
    add( Box.createHorizontalStrut( 5 ) );
    add( connectButton );
    add( Box.createHorizontalStrut( 5 ) );
    add( disconnectButton );
  }

  public String getUser()
  {
    return userIdTextPanel.getText().trim();
  }

  public void setFocusConnect()
  {
    getRootPane().setDefaultButton( connectButton );
  }

  public void clearText()
  {
    userIdTextPanel.clear();
  }

  public void actionPerformed(ActionEvent e) {
    int eventId = -1;
    if ( e.getSource() == connectButton )
    {
      eventId = CalEvent.CONNECT;
    }
    else if ( e.getSource() == disconnectButton )
    {
      eventId = CalEvent.DISCONNECT;
    }
    else if ( e.getSource() == userIdTextPanel )
    {
      eventId = CalEvent.FOCUS_CONNECT;
    }
    else return;

    CalEvent calEvent = new CalEvent( this, eventId );
    fireCalEvent( calEvent );
  }

  public void insertUpdate(DocumentEvent e)
  {
    int eventId = CalEvent.FOCUS_CONNECT;
    CalEvent calEvent = new CalEvent( this, eventId );
    fireCalEvent( calEvent );
  }
  
  public void removeUpdate(DocumentEvent e)
  {
    int eventId = CalEvent.FOCUS_CONNECT;
    CalEvent calEvent = new CalEvent( this, eventId );
    fireCalEvent( calEvent );
  }

  public void changedUpdate(DocumentEvent e)
  {
    throw new UnsupportedOperationException("Not supported yet.");
  }
}
